#include "tank.h"
void tank::inittank(int x,int y,int direct,int spe,bool is)
{{posx=x;posy=y;speed=spe;direction=direct;is_player=is;};
    timer =new QTimer;
    timer->start(15);
    connect(timer,SIGNAL(timeout()),SLOT(tank_update()));
    mybullet=new bullet;
    mybullet->setbullet(0,0,0,0);
}
int tank::is_move()
{if(posx<=200||posx>=280||posy<=440)
    {}
    else{maps::HQ=false;}
    int spe=speed;if(buff2){spe*=2;}
    int move=spe*(maps::mmap[(posy+19)/20][(posx+19)/20]+1);
    if(direction==1){
        if(posy-move<0){return posy;}

    if(maps::mmap[(posy-move)/20][posx/20]>=4||maps::mmap[(posy-move)/20][posx/20+1]>=4||maps::mmap[(posy-move)/20][(posx+39)/20]>=4)
    {

          return posy-((posy-move)/20)*20-20;


    }
    }
    if(direction==2)
    {
        if(posy+40+move>520){return 520-posy-40;}
      if(maps::mmap[(posy+40+move)/20][posx/20]>=4||maps::mmap[(posy+40+move)/20][posx/20+1]>=4||maps::mmap[(posy+40+move)/20][(posx+39)/20]>=4)
      {
          return ((posy+40+move)/20)*20-posy-40;
      }
    }
    if(direction==3)
    {if(posx-move<0){return posx;}

        if(maps::mmap[(posy)/20][(posx-move)/20]>=4||maps::mmap[(posy)/20+1][(posx+move)/20]>=4||maps::mmap[(posy+19)/20+1][(posx+move)/20]>=4)
    {
            return posx-((posx-move)/20)*20-20;
    }
    }
if(direction==4)
{if(posx+40+move>520){return 520-posx-40;}
    if(maps::mmap[posy/20][(posx+move+40)/20]>=4||maps::mmap[posy/20+1][(posx+move+40)/20]>=4||maps::mmap[(posy+19)/20+1][(posx+move+40)/20]>=4)
    {
        return ((posx+move+40)/20)*20-posx-40;
    }
}

return move;};
void tank::tank_update()
{
    move=true;
    if(buff2){buff2--;}
    if(buff3){buff3--;}
    touch_buff();
    if(mybullet->moving){
    mybullet->breakwall();
    mybullet->attackenemy(num);}
    if(mybullet->moving&&!is_player)
    {
        mybullet->attackplayer();
    }
    if(mybullet->moving)
    {
        switch(mybullet->direction)
        {
        case 1:
            mybullet->posy-=mybullet->speed;
            break;
        case 2:
            mybullet->posy+=mybullet->speed;
            break;
case 3:
            mybullet->posx-=mybullet->speed;
            break;
     case 4:
            mybullet->posx+=mybullet->speed;
        }
    }
}
void tank::shoot()
{
    if(mybullet->moving==false)
    {
        mybullet->moving=true;


        mybullet->setbullet(posx+18,posy+18,10,direction);
    }
};
int tank::touch_buff()

{int bu=0;
    for(int i=0;i<3;i++)
    {
        if(buff::buffpos[i][0])
        {
            if(buff::buffpos[i][1]==posx/20&&buff::buffpos[i][2]==posy/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            if(buff::buffpos[i][1]==(posx+39)/20&&buff::buffpos[i][2]==posy/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            if(buff::buffpos[i][1]==(posx+39)/20&&buff::buffpos[i][2]==(posy+39)/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            if(buff::buffpos[i][1]==posx/20&&buff::buffpos[i][2]==(posy+39)/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            //123
            if(buff::buffpos[i][1]==posx/20&&buff::buffpos[i][2]==(posy+20)/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            if(buff::buffpos[i][1]==(posx+20)/20&&buff::buffpos[i][2]==posy/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            if(buff::buffpos[i][1]==(posx+20)/20&&buff::buffpos[i][2]==(posy+20)/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            if(buff::buffpos[i][1]==(posx+20)/20&&buff::buffpos[i][2]==(posy+39)/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }
            if(buff::buffpos[i][1]==(posx+39)/20&&buff::buffpos[i][2]==(posy+20)/20)
            {bu=buff::buffpos[i][0];
                buff::buffpos[i][0]=0;
                break;
            }

        }

    }
    if(bu==1)
    {if(num==-1)
        {
            maps::playerhp=4;
        }
        else
        {
         is_life=3;
        }
       return 0;
    }
    if(bu==2)
    {
       buff2=600; return 0;
    }
if(bu==3)
{buff3=600;
}
if(buff3){
    if(num==-1&&maps::playerhp<4){maps::playerhp=4;}}
    return 0;}
